from requests import *
import time
from datetime import datetime
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.options.android import UiAutomator2Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# class CommonDriver:
#     def get_driver(self):
#         options = UiAutomator2Options()
#         options.platform_name = 'Android'
#         options.platform_version='7'
#         options.app_packge='com.dangdang.buy2'
#         options.app_activity='com.dangdnag.buy2.StarupActivity'
#         options.no_rest=False
#         options.unicode_keyboard=True
#         options.udidi='127.0.0.1:21503'
#
#         command_executor_url='http://127.0.0.1:4723/wd/hub'
#
#         driver =webdriver.Remote(command_executor=command_executor_url,options=options)
#         driver.implicitly_wait(10)
#         print("连接成功")
#         return driver
#
# if __name__ == '__main__':
#     CommonDriver().get_driver()


from appium.options.android import UiAutomator2Options
from appium import webdriver
from appium.webdriver.common.appiumby import *


class CommonDriver:
    def get_driver(self):
        options = UiAutomator2Options()
        options.platform_name = 'Android'
        options.platform_version = '7'
        # options.app_package = 'com.microvirt.launcher2'
        # options.app_activity = 'com.microvirt.launcher.Launcher'
        options.app_package = 'com.dangdang.buy2'
        options.app_activity = 'com.dangdang.buy2.StartupActivity'

        options.no_reset = False
        options.unicode_keyboard = True
        options.udid = '127.0.0.1:21513'

        command_executor_url = "http://127.0.0.1:4723/wd/hub"

        driver = webdriver.Remote(command_executor=command_executor_url, options=options)

        driver.implicitly_wait(10)

        print("连接成功")

        return driver


# if __name__ == '__main__':
#     driver = CommonDriver().get_driver()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.ViewFlipper[@resource-id="com.dangdang.buy2:id/home_view_flipper"]').click()
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').send_keys(
    #     "浪潮之巅")
    #
    # driver.find_element(AppiumBy.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_search"]').click()
    # try:
    #     screen_size=driver.get_window_size()
    #     height=screen_size["height"]
    #     print(height)
    #     width=screen_size["width"]
    #     print(width)
    #     start_x=width/2
    #     start_y=height*0.8
    #     end_x=width/2
    #     end_y=height*0.2
    #     driver.swipe(start_x,start_y,end_x,end_y)
    #     time.sleep(8)
    # finally:
    #     driver.quit()
    # driver.tap([(53,309)],500)
    # element=driver.find_element(AppiumBy.XPATH,'@text="当当"')
    # action=TouchAction(driver)
    # action=long_press(element).move_to(x=101,y=605).release().perform()